package com.wd.health.view.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.wd.health.R;
import com.wd.health.app.App;
import com.wd.health.model.adapter.KslbAdapter;
import com.wd.health.model.bean.BannerBean;
import com.wd.health.model.bean.KslbBean;
import com.wd.health.utils.RetrofitManager;
import com.wd.health.view.activity.home.CjbzActivity;
import com.wd.health.view.activity.mine.MineActivity;
import com.wd.health.view.activity.home.SearchActivity;
import com.wd.health.view.fragment.zxfragment.JkywFragment;
import com.wd.health.view.fragment.zxfragment.JsjfFragment;
import com.wd.health.view.fragment.zxfragment.YldtFragment;
import com.wd.health.view.fragment.zxfragment.YsmrFragment;
import com.wd.health.view.fragment.zxfragment.YxdtFragment;
import com.bumptech.glide.Glide;
import com.facebook.drawee.view.SimpleDraweeView;
import com.google.android.material.tabs.TabLayout;
import com.youth.banner.Banner;
import com.youth.banner.Transformer;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


public class HomeFragment extends Fragment {

    private Banner banner;
    private ArrayList<String> slist = new ArrayList<>();
    private RecyclerView rv_kslb;
    private TabLayout tl;
    private ViewPager vp;
    private List<String> list = new ArrayList<>();
    private List<Fragment> flist = new ArrayList<>();
    private LinearLayout ll_cjbz;
    private LinearLayout ll_cyyp;
    private EditText edit;
    private SimpleDraweeView sdv_user;
    private SharedPreferences user;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        initView(view);
        // Inflate the layout for this fragment
        return view;
    }

    private void initView(final View view) {
        edit = view.findViewById(R.id.edit_sousuo);
        banner = view.findViewById(R.id.banner_home);
        rv_kslb = view.findViewById(R.id.rv_wzzx);
        sdv_user = view.findViewById(R.id.sdv_user);
        rv_kslb.setLayoutManager(new GridLayoutManager(getContext(),4));
        tl = view.findViewById(R.id.tl_jkzx);
        tl.setSelectedTabIndicator(0);
        vp = view.findViewById(R.id.vp_jkzx);
        ll_cjbz = view.findViewById(R.id.ll_cjbz);
        ll_cyyp = view.findViewById(R.id.ll_cyyp);
        ll_cjbz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), CjbzActivity.class);
                startActivity(intent);
            }
        });
        ll_cyyp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), CjbzActivity.class);
                intent.putExtra("id",true);
                startActivity(intent);
            }
        });
        edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), SearchActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //轮播图
        initBanner();
        //科室列表
        initKslb();
        //健康资讯
        initJkzx();
        //判断登陆
        initLogin();
    }

    private void initSdv() {
        SharedPreferences user = App.getAppContext().getSharedPreferences("user", Context.MODE_PRIVATE);
        String headPic = user.getString("headPic", "");
        sdv_user.setImageURI(headPic);
    }

    private void initLogin() {
        user = getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
        int userId = user.getInt("userId", 0);
        initSdv();
        sdv_user.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), MineActivity.class);
                    startActivity(intent);
            }
        });
    }

    //健康资讯
    private void initJkzx() {
        if (list.size()<1){
            list.add("健康要闻");
            list.add("医学动态");
            list.add("医疗动态");
            list.add("养生美容");
            list.add("健身减肥");
            flist.add(new JkywFragment());
            flist.add(new YxdtFragment());
            flist.add(new YldtFragment());
            flist.add(new YsmrFragment());
            flist.add(new JsjfFragment());
        }
        vp.setAdapter(new MyPagerAdapter(getChildFragmentManager()));
        tl.setupWithViewPager(vp);
    }
    //pageradapter
    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(@NonNull FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return flist.get(position);
        }

        @Override
        public int getCount() {
            return flist.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return list.get(position);
        }
    }

    //科室列表
    private void initKslb() {
        RetrofitManager.getInstance().getKslbData(new Observer<KslbBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(KslbBean kslbBean) {
                List<KslbBean.ResultBean> list = kslbBean.getResult();
                KslbAdapter kslbAdapter = new KslbAdapter(list, getContext());
                rv_kslb.setAdapter(kslbAdapter);


            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }
    //轮播图
    private void initBanner() {
        RetrofitManager.getInstance().getBannerData(new Observer<BannerBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BannerBean bannerBean) {
                for (int i = 0; i < bannerBean.getResult().size(); i++) {
                    if (slist.size()<3){
                        slist.add(bannerBean.getResult().get(i).getImageUrl());
                    }
                }
                banner.setImages(slist);
                //设置轮播的动画效果
                banner.setBannerAnimation(Transformer.Default);
                //设置轮播间隔时间
                banner.setDelayTime(2000);
                //设置是否为自动轮播，默认是“是”。
                banner.isAutoPlay(true);
                banner.setImageLoader(new ImageLoader() {
                    @Override
                    public void displayImage(Context context, Object path, ImageView imageView) {
                        Glide.with(context).load(path).into(imageView);
                    }
                });
                banner.start();
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }
}